import * as fs from 'fs';

export class Log
{

    private static _writeStream:fs.WriteStream;

    public static init():void
    {
        let time = new Date().getTime();
        this._writeStream = fs.createWriteStream('./log/log' + time + '.txt');
    }

    private static parse(msg:string,params:any[]):string
    {
        let str = msg;
        for(let i = 0;i < params.length;i++)
        {
            str += ' ' + params[i];
        }
        return str;
    }

    private static write(data:string):void
    {
        let time = new Date().toString();
        this._writeStream.write(time + ':' + data + "\n");
    }

    public static log(msg:string,...params:any[]):void
    {
        //写入一个log.txt
        //每次运行写入一个文件
        let str = this.parse(msg,params);
        console.log(str);
        this.write(str);
    }

    public static error(msg:string,...params:any[]):void
    {
        console.error(msg,params);
        //写入一个error.txt
    }
    
    public static warn(msg:string,...params:any[]):void
    {
        console.warn(msg,params);
        //写入一个warn.txt
    }

    /**需要写入数据库的一部分内容 */
    public static msg(msg:string):void
    {
        console.log(msg);
        //存入数据库
    }
}